package com.live.util;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class Mail { 
	private MimeMessage mimeMsg;
	private Session session;
	private Properties props;
	private boolean needAuth = false;
	private String username; 
	private String password; 
	private Multipart mp;
	/**
	 * Constructor
	 * @param smtp 
	 */
	public Mail(String smtp){ 
		setSmtpHost(smtp); 
		createMimeMessage(); 
	} 

	/**
	 * 
	 * @param hostName String 
	 */
	public void setSmtpHost(String hostName) { 
		if(props == null)
			props = System.getProperties(); 
		props.put("mail.smtp.host",hostName);
	} 


	/**
	 * 
	 * @return
	 */
	public boolean createMimeMessage() 
	{ 
		try { 
			session = Session.getDefaultInstance(props,null); 
		} 
		catch(Exception e){ 
			return false; 
		} 
	
		try { 
			mimeMsg = new MimeMessage(session);
			mp = new MimeMultipart(); 
		
			return true; 
		} catch(Exception e){ 
			return false; 
		} 
	} 	
	
	/**
	 * @param need
	 */
	public void setNeedAuth(boolean need) { 
		if(props == null) props = System.getProperties(); 
		if(need){ 
			props.put("mail.smtp.auth","true"); 
		}else{ 
			props.put("mail.smtp.auth","false"); 
		} 
	} 

	/**
	 * @param name
	 * @param pass
	 */
	public void setNamePass(String name,String pass) { 
		username = name; 
		password = pass; 
	} 

	/**
	 * @param mailSubject
	 * @return
	 */
	public boolean setSubject(String mailSubject) { 
		try{ 
			mimeMsg.setSubject(mailSubject); 
			return true; 
		} 
		catch(Exception e) { 
			return false; 
		} 
	}
	
	/** 
	 * @param mailBody String 
	 */ 
	public boolean setBody(String mailBody) { 
		try{ 
			BodyPart bp = new MimeBodyPart(); 
			bp.setContent(""+mailBody,"text/html;charset=GBK"); 
			mp.addBodyPart(bp); 
		
			return true; 
		} catch(Exception e){ 
		return false; 
		} 
	} 
	/** 
	 * @param filename String 
	 */ 
	public boolean addFileAffix(String filename) { 
	
		try{ 
			BodyPart bp = new MimeBodyPart(); 
			FileDataSource fileds = new FileDataSource(filename); 
			bp.setDataHandler(new DataHandler(fileds)); 
			bp.setFileName(MimeUtility.encodeText(fileds.getName()));
			
			mp.addBodyPart(bp); 
			
			return true; 
		} catch(Exception e){ 
			return false; 
		} 
	} 
	
	/** 
	 * 璁剧疆鍙戜俊浜�
	 * @param from String 
	 */ 
	public boolean setFrom(String from) { 
		try{ 
			mimeMsg.setFrom(new InternetAddress(from)); //璁剧疆鍙戜俊浜� 
			return true; 
		} catch(Exception e) { 
			return false; 
		} 
	} 
	/** 
	 * @param to String 
	 */ 
	public boolean setTo(String to){ 
		if(to == null)return false; 
		try{ 
			mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to)); 
			return true; 
		} catch(Exception e) { 
			return false; 
		} 	
	} 
	
	/** 
	 * @param copyto String  
	 */ 
	public boolean setCopyTo(String copyto) 
	{ 
		if(copyto == null)return false; 
		try{ 
		mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto)); 
		return true; 
		} 
		catch(Exception e) 
		{ return false; } 
	} 

	
	/** 
	 */ 
	public boolean sendOut() 
	{ 
		try{ 
			mimeMsg.setContent(mp); 
			mimeMsg.saveChanges(); 
			
			Session mailSession = Session.getInstance(props,null); 
			Transport transport = mailSession.getTransport("smtp");
			
			transport.connect(String.valueOf(props.get("mail.smtp.host")),username,password);
			
			transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO)); 
		/*	transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.CC)); 
			//transport.send(mimeMsg); 
*/			
			transport.close(); 
			
			return true; 
		} catch(Exception e) { 
			return false; 
		} 
	} 

	/**
	 * 璋冪敤sendOut鏂规硶瀹屾垚閭欢鍙戦��
	 * @param smtp
	 * @param from
	 * @param to
	 * @param subject
	 * @param content
	 * @param username
	 * @param password
	 * @return boolean
	 */
	public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password) {
		Mail theMail = new Mail(smtp);
		theMail.setNeedAuth(true); //闇�瑕侀獙璇�
		
		if(!theMail.setSubject(subject)) return false;
		if(!theMail.setBody(content)) return false;
		if(!theMail.setTo(to)) return false;
		if(!theMail.setFrom(from)) return false;
		theMail.setNamePass(username,password);
		
		if(!theMail.sendOut()) return false;
		return true;
	}
	
	/**
	 * 璋冪敤sendOut鏂规硶瀹屾垚閭欢鍙戦��,甯︽妱閫�
	 * @param smtp
	 * @param from
	 * @param to
	 * @param copyto
	 * @param subject
	 * @param content
	 * @param username
	 * @param password
	 * @return boolean
	 */
	public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password) {
		Mail theMail = new Mail(smtp);
		theMail.setNeedAuth(true); //闇�瑕侀獙璇�
		
		if(!theMail.setSubject(subject)) return false;
		if(!theMail.setBody(content)) return false;
		if(!theMail.setTo(to)) return false;
		if(!theMail.setCopyTo(copyto)) return false;
		if(!theMail.setFrom(from)) return false;
		theMail.setNamePass(username,password);
		if(!theMail.sendOut()) return false;
		return true;
	}
	
	/**
	 * 璋冪敤sendOut鏂规硶瀹屾垚閭欢鍙戦��,甯﹂檮浠�
	 * @param smtp
	 * @param from
	 * @param to
	 * @param subject
	 * @param content
	 * @param username
	 * @param password
	 * @param filename 闄勪欢璺緞
	 * @return
	 */
	public static boolean send(String smtp,String from,String to,String subject,String content,String username,String password,String filename) {
		Mail theMail = new Mail(smtp);
		theMail.setNeedAuth(true); //闇�瑕侀獙璇�
		
		if(!theMail.setSubject(subject)) return false;
		if(!theMail.setBody(content)) return false;
		if(!theMail.addFileAffix(filename)) return false; 
		if(!theMail.setTo(to)) return false;
		if(!theMail.setFrom(from)) return false;
		theMail.setNamePass(username,password);
		
		if(!theMail.sendOut()) return false;
		return true;
	}
	
	/**
	 * 璋冪敤sendOut鏂规硶瀹屾垚閭欢鍙戦��,甯﹂檮浠跺拰鎶勯��
	 * @param smtp
	 * @param from
	 * @param to
	 * @param copyto
	 * @param subject
	 * @param content
	 * @param username
	 * @param password
	 * @param filename
	 * @return
	 */
	public static boolean sendAndCc(String smtp,String from,String to,String copyto,String subject,String content,String username,String password,String filename) {
		Mail theMail = new Mail(smtp);
		theMail.setNeedAuth(true); //闇�瑕侀獙璇�
		
		if(!theMail.setSubject(subject)) return false;
		if(!theMail.setBody(content)) return false;
		if(!theMail.addFileAffix(filename)) return false; 
		if(!theMail.setTo(to)) return false;
		if(!theMail.setCopyTo(copyto)) return false;
		if(!theMail.setFrom(from)) return false;
		theMail.setNamePass(username,password);
		
		if(!theMail.sendOut()) return false;
		return true;
	}
	
} 


